<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class news_topics extends Admin_Controller
{
	
	var $_table_name = TOPIC;	
	function lists()
	{
		global $CI;		
		$CI->load->module_model('news_topics.news_topics_list_model');
		//check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		// model		
		// sql
		$q = $CI->db->get($this->_table_name);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->news_topics_list_model->url_prefix.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->news_topics_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->select('*');
		$CI->db->from($this->_table_name);				    	
		foreach ($CI->news_topics_list_model->columns as $k=>$v)
		{
			if ($CI->news_topics_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->news_topics_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->news_topics_list_model->start);
		$q = $CI->db->get();		
		
		
		// gridview
		$items = $q->result();
		$this->add('items', $items);
		$this->add('start', $CI->news_topics_list_model->start);
		$this->add('language', $language?$language:'');
		foreach ($CI->news_topics_list_model->columns as $k=>$v)
		{
			if ($CI->news_topics_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->news_topics_list_model->cache->load($k).'"');
		}

		// message
		$this->add('msg', $CI->news_topics_list_model->cache->load($CI->news_topics_list_model->msg_cache_prefix));
		$CI->news_topics_list_model->cache->add($CI->news_topics_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('news_topics.toolbar', array('parent_id'=>0, 'start'=>$CI->news_topics_list_model->start, 'language'=>$language));
		$this->module_navigation('Quản lý nhóm tin ');
		
		// view
		$this->view('news_topics_list_view');
	}
	
	function detail()
	{
		global $CI;
		$CI->load->module_model('news_topics.news_topics_detail_model');
		//check language
		$language = $CI->uri->segment(3);
		if($language=="english"){
			$this->_table_name = $this->_table_name."_en";			
		}
		$this->add('language', $language?$language:'');
		// model
		
		
		$q = $CI->db->get_where($this->_table_name, array(TOPIC_ID=>$CI->news_topics_detail_model->item));
		$news_topics = $q->row();
		
			
		// error
		$post = $CI->news_topics_detail_model->cache->load('post');
		$CI->news_topics_detail_model->cache->add('post', '');

		// add to view
		$this->add('name', $post?$post['name']:($news_topics?$news_topics->{TOPIC_NAME}:''));
		$this->add('start', $CI->news_topics_detail_model->start);
		$this->add('item', $CI->news_topics_detail_model->item);
		
		
		// editor
		$CI->load->library('fckeditor');
		$CI->fckeditor->InstanceName = 'description';
		$CI->fckeditor->Value = $post?$post['description']:($news_topics?$news_topics->{TOPIC_DESCRIPTION}:'');
		$CI->fckeditor->SetPathOfFranchise();
		$this->add('description', $CI->fckeditor->CreateHtml());
				
		// message
		$this->add('msg', $CI->news_topics_detail_model->cache->load($CI->news_topics_detail_model->msg_cache_prefix));
		$CI->news_topics_detail_model->cache->add($CI->news_topics_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->news_topics_detail_model->item)
			$this->module_navigation('Nhóm tin : HIệu chỉnh');
		else
			$this->module_navigation('Nhóm tin : Thêm mới');
			
		// view
		$this->view('news_topics_detail_view');
	}
	
}

?>